package com.ruoyi.recycle.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.recycle.domain.ClientUserManagement;
import com.ruoyi.recycle.domain.CommentManagement;
import com.ruoyi.recycle.domain.OrderEvaluate;
import com.ruoyi.recycle.dto.CommentManagementDTO;
import com.ruoyi.recycle.dto.OrderEvaluateDTO;
import com.ruoyi.recycle.service.ICommentManagementService;
import com.ruoyi.recycle.service.IOrderEvaluateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

/**
 * 评论管理Controller
 *
 * @author Wubk
 * @date 2022-05-22
 */
@RestController
@RequestMapping("/comment")
@Api(tags = "评论管理")
public class CommentManagementController extends BaseController {
    @Autowired
    private ICommentManagementService commentManagementService;
    @Resource
    private IOrderEvaluateService orderEvaluateService;

    /**
     * 查询评论管理列表
     */
    @RequiresPermissions("recycle:comment:list")
    @GetMapping("/list")
    public TableDataInfo list(CommentManagement commentManagement) {
        startPage();
        List<CommentManagement> list = commentManagementService.selectCommentManagementList(commentManagement);
        return getDataTable(list);
    }

    /**
     * 分页查询评论管理列表
     */
    @PostMapping("/commentPageQuery")
    @RequiresPermissions("recycle:comment:list")
    @ApiOperation("分页查询评论管理列表")
    public IPage<CommentManagement> commentPageQuery(@RequestBody CommentManagementDTO queryDTO) throws Exception{
        final CommentManagement factory = new CommentManagement();
        BeanUtils.copyProperties(queryDTO, factory);
        final LambdaQueryWrapper<CommentManagement> wrapper = Wrappers.lambdaQuery(factory);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat old = new SimpleDateFormat("yyyy-MM-dd");
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(CommentManagement::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeBegin()) + " 00:00:00"));

        }
        if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(CommentManagement::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeEnd()) + " 23:59:59"));

        }
        wrapper.orderByDesc(CommentManagement::getCreatedTime);
        return commentManagementService.page(new Page<CommentManagement>()
                .setCurrent(queryDTO.getCurrentPage())
                .setSize(queryDTO.getPageSize()), wrapper);
    }

    /**
     * 分页查询回收与积分订单管理列表
     */
    @PostMapping("/orderEvaluatePageQuery")
    @ApiOperation("分页查询评论管理列表")
    public IPage<OrderEvaluate> commentPageQuery(@RequestBody OrderEvaluateDTO queryDTO) throws Exception{
        final OrderEvaluate evaluate = new OrderEvaluate();
        BeanUtils.copyProperties(queryDTO, evaluate);
        final LambdaQueryWrapper<OrderEvaluate> wrapper = Wrappers.lambdaQuery(evaluate);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat old = new SimpleDateFormat("yyyy-MM-dd");
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(OrderEvaluate::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeBegin()) + " 00:00:00"));
        }
        if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(OrderEvaluate::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeEnd()) + " 23:59:59"));

        }
        wrapper.orderByDesc(OrderEvaluate::getCreatedTime);
        return orderEvaluateService.page(new Page<OrderEvaluate>()
                .setCurrent(queryDTO.getCurrentPage())
                .setSize(queryDTO.getPageSize()), wrapper);
    }

    /**
     * 删除回收与积分订单管理列表
     */
    @Log(title = "删除回收与积分订单管理列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteOrderEvaluate/{ids}")
    @ApiOperation("删除回收与积分订单管理列表")
    @ApiImplicitParam(name = "ids", value = "id集合", dataType = "List")
    public AjaxResult deleteOrderEvaluate(@PathVariable Long[] ids) {

        orderEvaluateService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }



    /**
     * 导出评论管理列表
     */
    @RequiresPermissions("recycle:comment:export")
    @Log(title = "评论管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出评论管理列表")
    public void export(HttpServletResponse response, CommentManagement commentManagement) {
        List<CommentManagement> list = commentManagementService.selectCommentManagementList(commentManagement);
        ExcelUtil<CommentManagement> util = new ExcelUtil<CommentManagement>(CommentManagement.class);
        util.exportExcel(response, list, "评论管理数据");
    }

    /**
     * 获取评论管理详细信息
     */
    @RequiresPermissions("recycle:comment:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取评论管理详细信息")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(commentManagementService.selectCommentManagementById(id));
    }

    /**
     * 新增评论管理
     */
    @RequiresPermissions("recycle:comment:add")
    @Log(title = "评论管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增评论管理")
    public AjaxResult add(@RequestBody CommentManagement commentManagement) {
        return toAjax(commentManagementService.insertCommentManagement(commentManagement));
    }

    /**
     * 修改评论管理
     */
    @RequiresPermissions("recycle:comment:edit")
    @Log(title = "评论管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改评论管理")
    public AjaxResult edit(@RequestBody CommentManagement commentManagement) {
        return toAjax(commentManagementService.updateCommentManagement(commentManagement));
    }

    /**
     * 删除评论管理
     */
    @RequiresPermissions("recycle:comment:remove")
    @Log(title = "评论管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除评论管理")
    @ApiImplicitParam(name = "ids", value = "id集合", dataType = "List")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(commentManagementService.deleteCommentManagementByIds(ids));
    }
}
